package lib.quasar.widget.web;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import lib.quasar.util.DimenUtil;

/**
 * description: 字体加粗
 * created by kalu on 2017/5/17 10:30
 */
public class CusWebTitle extends AppCompatTextView {

    /*************************************/

    private int drawWidth = 0;

    public CusWebTitle(Context context) {
        this(context, null, 0);
    }

    public CusWebTitle(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CusWebTitle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

//        paintLine = new Paint();
//        paintLoad = new Paint();
        // 解决TextView高度和textSize大小不一致
        // setIncludeFontPadding(false);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        getPaint().setFakeBoldText(true);
        super.setText(text, type);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        Paint paint = getPaint();

        // 横线
        paint.clearShadowLayer();
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(Color.parseColor("#66e6e6e6"));
        float top = getHeight() - DimenUtil.dp2px(1);
        float bottom = getHeight();
        canvas.drawRect(0, top, getWidth(), bottom, paint);

        // 进度条
        paint.clearShadowLayer();
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        float top2 = getHeight() - DimenUtil.dp2px(3);
        float bottom2 = getHeight() - DimenUtil.dp2px(1);
        if (drawWidth == getWidth()) {
            paint.setColor(Color.TRANSPARENT);
        } else {
            // LinearGradient lg = new LinearGradient(0, 0, width, 2, Color.RED, Color.DKGRAY, Shader.TileMode.MIRROR);
            // paint.setShader(lg);
            paint.setColor(Color.RED);
        }
        canvas.drawRect(0, top2, drawWidth, bottom2, paint);
    }

    public void drawLoad(int progress) {

        drawWidth = (getWidth() * progress / 100);
        postInvalidate();
    }
}
